package cn.lmxdev.ptms.controller;

import cn.lmxdev.ptms.bean.table.TUser;
import cn.lmxdev.ptms.bean.table.TUserRole;
import cn.lmxdev.ptms.bean.vo.ResponseVo;
import cn.lmxdev.ptms.service.IUserRoleServlet;
import cn.lmxdev.ptms.service.IUserServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController(value = "userRoleController")
@RequestMapping(value = "/api/user-role")
public class UserRoleController {
    private final IUserRoleServlet userRoleServlet;
    private final IUserServlet userServlet;

    @Autowired
    public UserRoleController(
            IUserRoleServlet userRoleServlet,
            IUserServlet userServlet
    ) {
        this.userRoleServlet = userRoleServlet;
        this.userServlet = userServlet;
    }

    @GetMapping(value = "/my-role")
    public ResponseVo<Object> myRole() {
        // 准备数据
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        TUser nowUser = this.userServlet.getByAccount(auth.getName());

        // 响应数据
        return new ResponseVo<>(nowUser.getUserRole());
    }

    @GetMapping(value = "/list")
    public ResponseVo<Object> list(
            @RequestParam(value = "limit", required = false) Long limit,
            @RequestParam(value = "page", required = false) Long page,
            TUserRole userRole
    ) {
        return new ResponseVo<>(
                this.userRoleServlet.count(userRole),
                this.userRoleServlet.list(limit, page, userRole)
        );
    }
}
